<template>
  <div class="register-page">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
    
    <!-- 主要内容 -->
    <div class="register-container">
      <!-- 左侧注册面板 -->
      <div class="register-panel">
        <div class="register-content">
          <!-- 标题 -->
          <div class="register-header">
            <h2 class="register-title">注册</h2>
            <p class="register-subtitle">开始您的心理健康之旅，我们将为您保护隐私并提供专业服务</p>
          </div>
          
          <!-- 注册表单 -->
          <div class="register-form">
            <div class="camera-section">
              <!-- 人脸拍摄组件 -->
              <div class="camera-wrapper">
                <CameraCapture
                  @photo-confirmed="handlePhotoConfirmed"
                  @retake-photo="handleRetakePhoto"
                  :width="400"
                  :height="300"
                  :show-external-buttons="true"
                  :reset="!hasPhoto"
                  class="camera-component"
                />
              </div>
              
              <!-- 按钮区域 -->
              <div class="action-section" v-if="hasPhoto">
                <div class="button-row">
                  <el-button
                    type="success"
                    size="large"
                    @click="handleRegister"
                    :loading="registering"
                    class="action-btn register-btn"
                  >
                    <el-icon v-if="!registering"><UserFilled /></el-icon>
                    {{ registering ? '注册中...' : '立即注册' }}
                  </el-button>
                  <el-button
                    type="success"
                    size="large"
                    @click="handleRetakePhoto"
                    class="action-btn retake-btn"
                  >
                    <el-icon><RefreshRight /></el-icon>
                    重新拍摄
                  </el-button>
                </div>
              </div>
              
            </div>
            
          </div>
        </div>
      </div>
      
      <!-- 右侧信息面板 -->
      <div class="info-panel">
        <div class="brand-section">
          <div class="brand-icon">
            <el-icon size="48"><UserFilled /></el-icon>
          </div>
          <h1 class="brand-title">暖星心里守护站</h1>
          <p class="brand-subtitle">专业·安全·贴心</p>
        </div>
        
        <div class="features-section">
          <div class="feature-item">
            <el-icon class="feature-icon"><Camera /></el-icon>
            <div class="feature-content">
              <h3>专业服务</h3>
              <p>资深心理治疗师团队，为您提供个性化的治疗方案</p>
            </div>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><Lock /></el-icon>
            <div class="feature-content">
              <h3>隐私保护</h3>
              <p>严格遵守医疗隐私法规，您的信息绝对安全保密</p>
            </div>
          </div>
          <div class="feature-item">
            <el-icon class="feature-icon"><Lightning /></el-icon>
            <div class="feature-content">
              <h3>温暖陪伴</h3>
              <p>提供24小时在线支持，让您在治疗路上不再孤单</p>
            </div>
          </div>
        </div>
        
        <div class="bottom-info">
          <p>已有账户？<router-link to="/login" class="link">立即登录</router-link></p>
          <p>医生用户？<router-link to="/doctor/register" class="link">账号注册</router-link></p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

import { UserFilled, Camera, Lock, Lightning, RefreshRight } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import CameraCapture from '../../components/CameraCapture.vue'
import { faceRegister } from '../../api/face.js'

const router = useRouter()
const hasPhoto = ref(false)
const photoFile = ref(null)
const registering = ref(false)

const handlePhotoConfirmed = (blob) => {
  photoFile.value = blob
  hasPhoto.value = true
}

const handleRetakePhoto = () => {
  hasPhoto.value = false
  photoFile.value = null
  // 通知CameraCapture组件重新拍摄
}

const handleRegister = async () => {
  if (!photoFile.value) {
    return
  }
  
  registering.value = true
  
  try {
    const formData = new FormData()
    formData.append('faceImage', photoFile.value, 'face.jpg')
    
    const response = await faceRegister(formData)
    
    // 检查是否是从医生注册页面跳转过来的
    const urlParams = new URLSearchParams(window.location.search)
    const isForDoctor = urlParams.get('for') === 'doctor'
    
    if (isForDoctor && response.data && response.data.id) {
      // 医生注册流程 - 跳转到医生注册页面并传递人脸ID
      ElMessage.success({
        message: '人脸注册成功！正在跳转到医生注册页面...',
        duration: 2000
      })
      
      // 保存人脸ID到localStorage，以防URL参数丢失
      localStorage.setItem('tempFaceInfoId', response.data.id.toString())
      
      setTimeout(() => {
        router.push(`/doctor/register?faceInfoId=${response.data.id}`)
      }, 1500)
    } else {
      // 普通用户注册流程 - 跳转到登录页面
      ElMessage.success({
        message: '注册成功！正在跳转到登录页面...',
        duration: 2000
      })
      
      setTimeout(() => {
        router.push('/login')
      }, 1500)
    }
    
  } catch (error) {
    console.error('Register error:', error)
    
    ElMessage.error({
      message: error.message || '注册失败，请重试',
      duration: 3000
    })

  } finally {
    registering.value = false
  }
}

const resetRegister = () => {
  hasPhoto.value = false
  photoFile.value = null
}
</script>

<style scoped>
.register-page {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: 
    /* 主渐变背景 - 成长希望色调 */
    linear-gradient(120deg, 
      rgba(129, 199, 132, 0.9) 0%,   /* 生机绿色 */
      rgba(165, 214, 167, 0.8) 30%,  /* 温和绿色 */
      rgba(79, 195, 247, 0.8) 70%,   /* 希望蓝色 */
      rgba(100, 181, 246, 0.9) 100%  /* 梦想蓝色 */
    ),
    /* 成长花朵图案 */
    url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="flowers" width="100" height="100" patternUnits="userSpaceOnUse"><g opacity="0.12"><circle cx="25" cy="25" r="3" fill="white"/><circle cx="35" cy="20" r="2" fill="white" opacity="0.6"/><circle cx="20" cy="35" r="2" fill="white" opacity="0.6"/><circle cx="30" cy="30" r="1.5" fill="white" opacity="0.8"/><path d="M25,25 L30,20 L35,25 L30,30 Z" fill="white" opacity="0.3"/><circle cx="75" cy="75" r="4" fill="white" opacity="0.4"/><circle cx="85" cy="70" r="2.5" fill="white" opacity="0.3"/><circle cx="70" cy="85" r="2" fill="white" opacity="0.5"/><path d="M75,75 Q80,70 85,75 Q80,80 75,75" fill="white" opacity="0.2"/><circle cx="50" cy="10" r="2" fill="white" opacity="0.4"/><circle cx="10" cy="90" r="1.5" fill="white" opacity="0.3"/><circle cx="90" cy="10" r="1" fill="white" opacity="0.5"/></g></pattern></defs><rect width="100%" height="100%" fill="url(%23flowers)"/></svg>'),
    /* 温暖光晕效果 */
    radial-gradient(circle at 30% 20%, rgba(255,255,255,0.2) 0%, transparent 45%),
    radial-gradient(circle at 70% 80%, rgba(165, 214, 167,0.25) 0%, transparent 50%),
    radial-gradient(circle at 90% 30%, rgba(79, 195, 247,0.18) 0%, transparent 40%),
    /* 底层柔和渐变 */
    linear-gradient(60deg, #f1f8e9 0%, #e8f4fd 100%);
  background-size: 
    100% 100%,           /* 主渐变 */
    100px 100px,         /* 花朵图案 */
    250px 250px,         /* 光晕1 */
    380px 380px,         /* 光晕2 */
    320px 320px,         /* 光晕3 */
    100% 100%;           /* 底层渐变 */
  background-repeat: no-repeat;
  background-position: center center;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-x: auto;
  overflow-y: auto;
  z-index: 0;
}

.background-decoration {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(120deg, 
    rgba(255, 255, 255, 0.18) 0%, 
    rgba(165, 214, 167, 0.12) 40%, 
    rgba(79, 195, 247, 0.12) 100%
  );
  backdrop-filter: blur(30px);
  border: 1px solid rgba(255, 255, 255, 0.25);
  animation: growthFloat 9s ease-in-out infinite;
  box-shadow: 
    0 12px 40px rgba(129, 199, 132, 0.2),
    inset 0 2px 0 rgba(255, 255, 255, 0.4);
}

.circle-1 {
  width: 160px;
  height: 160px;
  top: 12%;
  right: 8%;
  animation-delay: 0s;
}

.circle-2 {
  width: 110px;
  height: 110px;
  bottom: 18%;
  left: 12%;
  animation-delay: 3s;
}

.circle-3 {
  width: 85px;
  height: 85px;
  top: 48%;
  left: 4%;
  animation-delay: 6s;
}

/* 添加花朵装饰效果 */
.circle::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40%;
  height: 40%;
  border-radius: 50%;
  background: radial-gradient(circle, 
    rgba(255, 255, 255, 0.4) 0%, 
    rgba(165, 214, 167, 0.2) 50%,
    transparent 80%
  );
  transform: translate(-50%, -50%);
}

.circle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20%;
  height: 20%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.6);
  transform: translate(-50%, -50%);
}

@keyframes growthFloat {
  0%, 100% { 
    transform: translateY(0px) rotate(0deg) scale(1); 
    opacity: 0.7;
  }
  25% { 
    transform: translateY(-12px) rotate(90deg) scale(1.08); 
    opacity: 0.9;
  }
  50% { 
    transform: translateY(-20px) rotate(180deg) scale(0.92); 
    opacity: 0.8;
  }
  75% { 
    transform: translateY(-8px) rotate(270deg) scale(1.05); 
    opacity: 0.85;
  }
}

.register-container {
  position: relative;
  z-index: 2;
  display: flex;
  width: min(1200px, 95vw);
  min-width: 300px;
  height: min(700px, 90vh);
  min-height: 500px;
  background: 
    linear-gradient(120deg, rgba(255, 255, 255, 0.98) 0%, rgba(249, 253, 251, 0.96) 100%);
  border-radius: 24px;
  box-shadow: 
    0 25px 50px rgba(0, 0, 0, 0.12),
    0 0 0 1px rgba(165, 214, 167, 0.15),
    inset 0 1px 0 rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(25px);
  overflow: hidden;
  margin: 20px;
  border: 1px solid rgba(255, 255, 255, 0.35);
}

.register-panel {
  flex: 1;
  padding: 60px 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-content {
  width: 100%;
  max-width: 480px;
}

.register-header {
  text-align: center;
  margin-bottom: 40px;
}

.register-title {
  font-size: 28px;
  font-weight: 700;
  color: #333;
  margin: 0 0 10px 0;
}

.register-subtitle {
  color: #666;
  font-size: 16px;
  margin: 0;
}

.camera-section {
  text-align: center;
}

.camera-wrapper {
  margin-top: 25px;
  margin-bottom: 30px;
  padding-top: 20px;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}

.camera-component {
  border-radius: 16px;
}

.action-section {
  margin-bottom: 30px;
}

.button-row {
  display: flex;
  gap: 15px;
  justify-content: center;
}

.action-btn {
  flex: 1;
  height: 50px;
  border-radius: 25px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #81C784 0%, #4FC3F7 100%);
  border: none;
  box-shadow: 0 4px 15px rgba(129, 199, 132, 0.4);
  transition: all 0.3s ease;
  max-width: 180px;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(129, 199, 132, 0.6);
}



.info-panel {
  flex: 1;
  background: linear-gradient(135deg, #81C784 0%, #4FC3F7 100%);
  color: white;
  padding: 60px 50px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  position: relative;
}

.info-panel::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grid" width="10" height="10" patternUnits="userSpaceOnUse"><path d="M 10 0 L 0 0 0 10" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="0.5"/></pattern></defs><rect width="100" height="100" fill="url(%23grid)"/></svg>');
  opacity: 0.3;
}

.brand-section {
  position: relative;
  z-index: 1;
  text-align: center;
}

.brand-icon {
  margin-bottom: 20px;
}

.brand-title {
  font-size: 32px;
  font-weight: 700;
  margin: 0 0 10px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-subtitle {
  font-size: 18px;
  opacity: 0.9;
  margin: 0;
}

.features-section {
  position: relative;
  z-index: 1;
}

.feature-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 30px;
}

.feature-icon {
  margin-right: 20px;
  margin-top: 5px;
  font-size: 24px;
  opacity: 0.9;
}

.feature-content h3 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 8px 0;
}

.feature-content p {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
  line-height: 1.5;
}

.bottom-info {
  position: relative;
  z-index: 1;
  text-align: center;
  font-size: 16px;
  opacity: 0.9;
}

.link {
  color: white;
  text-decoration: none;
  font-weight: 600;
  border-bottom: 1px solid rgba(255, 255, 255, 0.5);
  transition: all 0.3s ease;
}

.link:hover {
  border-bottom-color: white;
}

/* 响应式设计 */
@media (max-width: 1280px) {
  .register-container {
    width: min(1000px, 95vw);
    height: min(600px, 90vh);
  }
  
  .register-panel, .info-panel {
    padding: 40px 35px;
  }
  
  .brand-title {
    font-size: 28px;
  }
}

/* 超小屏幕和极端缩放优化 */
@media (max-width: 480px), (max-height: 480px) {
  .register-page {
    padding: 10px;
  }
  
  .register-container {
    width: 100%;
    min-width: 280px;
    height: auto;
    min-height: 400px;
    margin: 10px;
    border-radius: 16px;
  }
  
  .register-panel, .info-panel {
    padding: 20px 15px;
  }
  
  .brand-title {
    font-size: 22px;
  }
  
  .register-title {
    font-size: 20px;
  }
  
  .camera-wrapper {
    margin-top: 15px;
    margin-bottom: 15px;
    padding-top: 15px;
  }
  
  .features-section {
    display: none;
  }
}

@media (max-width: 1024px) {
  .register-container {
    flex-direction: column;
    width: min(500px, 95vw);
    height: auto;
  }
  
  .register-panel {
    padding: 40px 35px 20px 35px;
  }
  
  .info-panel {
    padding: 20px 35px 40px 35px;
  }
  
  .features-section {
    display: none;
  }
}

@media (max-width: 768px) {
  .register-container {
    width: 95%;
    margin: 20px;
    border-radius: 16px;
  }
  
  .register-panel, .info-panel {
    padding: 30px 25px;
  }
  
  .camera-wrapper {
    margin-top: 20px;
    margin-bottom: 20px;
    padding-top: 18px;
  }
}
</style>
